package org.softmed.ATComm.swingClient;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class SMSPanel extends JPanel implements ActionListener {

	JTextField number = new JTextField();
	JTextArea message = new JTextArea();
	JScrollPane scroll;
	JButton send = new JButton("Send");
	JButton clear = new JButton("Clear");

	int numberWidth = 512;
	int numberHeight = 25;

	int scrollWidth = 512;
	int scrollHeight = 100;
	private ClientManager clientManager;

	public SMSPanel() {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		number.setMaximumSize(new Dimension(numberWidth, numberHeight));
		number.setMinimumSize(new Dimension(numberWidth, numberHeight));
		number.setPreferredSize(new Dimension(numberWidth, numberHeight));

		JPanel temp = new JPanel();
		temp.setLayout(new BoxLayout(temp, BoxLayout.LINE_AXIS));
		temp.add(new JLabel(
				"Number - don't forget the prefix : Portugal is +351"));
		temp.add(Box.createHorizontalGlue());

		add(temp);
		add(number);

		temp = new JPanel();
		temp.setLayout(new BoxLayout(temp, BoxLayout.LINE_AXIS));
		temp.add(new JLabel(
				"Message - warning: All new line characters will be removed"));
		temp.add(Box.createHorizontalGlue());

		add(temp);
		scroll = new JScrollPane(message);

		scroll.setMaximumSize(new Dimension(scrollWidth, scrollHeight));
		scroll.setMinimumSize(new Dimension(scrollWidth, scrollHeight));
		scroll.setPreferredSize(new Dimension(scrollWidth, scrollHeight));

		add(scroll);

		JPanel bottom = new JPanel();
		bottom.setLayout(new BoxLayout(bottom, BoxLayout.LINE_AXIS));
		bottom.add(clear);
		bottom.add(Box.createHorizontalGlue());
		bottom.add(send);

		add(bottom);

		clear.addActionListener(this);
		send.addActionListener(this);
	}

	public String getNumber() {
		return number.getText();
	}

	public String getMessage() {
		String text = message.getText();
		text = text.replace('\n', ' ');
		return text;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();
		if (source == clear) {
			number.setText("");
			message.setText("");
		} else if (source == send) {
			clientManager.sendMessage();
		}

	}

	public void setClientManager(ClientManager clientManager) {
		this.clientManager = clientManager;

	}

}
